using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;

namespace MediaBackup
{
    // BrianSp: This class manages the service when run in that mode.
    public partial class Service : ServiceBase
    {
        private static Service s_Service;

        public Service()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            s_Service = this;

            // BrianSp: Kick off a new thread because the service manager owns this one.  This thread
            // runs the main application entry point.
            Thread thread = new Thread(new ThreadStart(StaticServiceRun));
            thread.Start();
        }

        private static void StaticServiceRun()
        {
            s_Service.ServiceRun();
        }

        private void ServiceRun()
        {
            try
            {
                Application.Run();
            }
            catch
            {
                Stop();
            }
        }

        protected override void OnStop()
        {
            // BrianSp: If the service control manager sends a stop message, set the event to let the app thread
            // know it's time to shutdown.
            Application.s_End.Set();
        }
    }
}
